<?php
/**
 * 课程管理
 * User: liaohonglai
 * Date: 2017/10/25
 * Time: 11:22
 */

namespace app\admin\controller;
use \think\cache\driver\File;
use think\Controller;
use app\admin\model\Course as myCourse;
use think\Db;
use think\Request;
class Course extends Controller
{
    /**
     * 去界面
     * @return mixed
     */
    public function index(){
        return $this->fetch();
    }

    /**
     * 分页and条件查询
     * @param int $pageNumber
     * @param int $pageSize
     * @param string $searchText
     * @return \think\response\Json
     */
    public function getList($pageNumber=0, $pageSize=5, $searchText='%'){
        $course = new myCourse();

        $data = $course->where("id = :id or name LIKE :name",['id'=>$searchText,'name'=>'%'.$searchText.'%'])->paginate($pageSize,false,['page'=>$pageNumber]);

        return json( $data);
    }

    /**
     * 课程添加
     * @param Request $request
     * @return \think\response\Json
     */
    public function addCourse(Request $request) {
        if (file_exists(ROOT_PATH . 'public' . DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"]))
        {
            return json(['success'=>false, "msg"=>$_FILES["courseimg"]["name"] . '文件已经存在']);
        }
        else
        {
            // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
            move_uploaded_file($_FILES["courseimg"]["tmp_name"], ROOT_PATH . 'public' . DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"]);

            $data = $request->only(['name', 'code', 'note']);
            $data['courseimg'] = DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"];
            $result = myCourse::create($data);
            if ($result) {
                return json(['success'=>true, "msg"=>'添加成功']);
            }
        }
        return json(['success'=>false, "msg"=>'添加失败']);

    }

    /**
     * 课程修改
     * @param Request $request
     * @return \think\response\Json
     */
    public function updateCourse(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['id','name','code','note']);
        // 更新用户
        //$result = DB::table($this->table)->update($user);
        $result = myCourse::update($data);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'修改成功']);
        } else {
            return json(['success'=>false, "msg"=>'修改失败']);
        }
    }

    /**
     * 删除课程
     * @return \think\response\Json
     */
    public function delete() {
        $id = input('id');
        $courseimg = input('courseimg'); //  "\\static\\uploads\/5.jpg"  -> static/uploads/5.jpg

        $newstr = str_replace("\\","/", $courseimg);
        $sub = substr($newstr,1);

        // 删除用户
        //$result = DB::table($this->table)->where("id", $id)->delete();
        $result = null;
        if (unlink($sub)){
            $result = myCourse::destroy($id);
        }

        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'删除成功']);
        } else {
            return json(['success'=>false, "msg"=>'删除用户不存在']);
        }
    }

    //图片修改
    public function updataImage(Request $request){
            // D:\\Program Files\\Apache2.4.28\\Apache24\\htdocs\\oes\\public\\static\\uploads\/3.jpg
         if (file_exists(ROOT_PATH . 'public' . DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"]))
        {
            return json(['success'=>false, "msg"=>$_FILES["courseimg"]["name"] . '图片已被使用']);
        }
        else
        {
            // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
            move_uploaded_file($_FILES["courseimg"]["tmp_name"], ROOT_PATH . 'public' . DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"]);

            $data['id'] = $request->param('id');
            $data['courseimg'] = DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"];
            $result = myCourse::update($data);
            if ($result) {
                $dataq = $request->param('imgPath');
                $newstr = str_replace('\\',"/", $dataq);
                $sub = substr($newstr,1);
                // D:\\Program Files\\Apache2.4.28\\Apache24\\htdocs\\oes\\public\\static\\uploads\/banner1.jpg
                if (file_exists(ROOT_PATH . 'public'. $dataq)){
                    unlink($sub);
                    return json(['success'=>true, "msg"=>'修改成功',"courseimg"=>DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"]]);

                }

                return json(['success'=>true, "msg"=>'修改成功',"courseimg"=>DS . 'static'. DS . 'uploads/' . $_FILES["courseimg"]["name"]]);

            }else{
                return json(['success'=>false, "msg"=>'修改失败']);
            }
        }
    }
}